// 测试1102指令响应格式
const WebSocket = require('ws');

class Code1102ResponseTester {
  constructor() {
    this.ws = null;
  }

  async connect () {
    return new Promise((resolve, reject) => {
      console.log('🔌 连接到WebSocket服务器...');

      this.ws = new WebSocket('ws://localhost:3000/user-service/websocket');

      this.ws.on('open', () => {
        console.log('✅ WebSocket连接成功');
        resolve();
      });

      this.ws.on('error', (error) => {
        console.error('❌ WebSocket连接失败:', error.message);
        reject(error);
      });
    });
  }

  async test1102Response () {
    console.log('\n📄 测试1102指令响应格式...');

    return new Promise((resolve) => {
      let responseReceived = false;

      const responseHandler = (data) => {
        try {
          const message = JSON.parse(data.toString());
          if (message.act_id === 1102) {
            this.ws.removeListener('message', responseHandler);
            responseReceived = true;

            console.log('📊 1102响应数据:');
            console.log(JSON.stringify(message, null, 2));

            // 检查响应格式
            console.log('\n🔍 响应格式分析:');

            const hasActId = 'act_id' in message;
            const hasCode = 'code' in message;
            const hasList = 'list' in message;
            const hasPage = 'page' in message;
            const hasSum = 'sum' in message;

            console.log(`- act_id: ${hasActId ? '✅' : '❌'} (值: ${message.act_id})`);
            console.log(`- code: ${hasCode ? '✅' : '❌'} (值: ${message.code})`);
            console.log(`- list: ${hasList ? '✅' : '❌'} (类型: ${Array.isArray(message.list) ? '数组' : typeof message.list}, 长度: ${message.list ? message.list.length : 0})`);
            console.log(`- page: ${hasPage ? '✅' : '❌'} (值: ${message.page})`);
            console.log(`- sum: ${hasSum ? '✅' : '❌'} (值: ${message.sum})`);

            if (message.code === 0) {
              console.log('\n✅ 1102指令响应格式正确！');
              console.log('📋 分页信息:');
              console.log(`  - 当前页码: ${message.page}`);
              console.log(`  - 总数量: ${message.sum}`);
              console.log(`  - 当前页数据量: ${message.list ? message.list.length : 0}`);
            } else {
              console.log(`\n❌ 1102指令执行失败: ${message.msg || '未知错误'}`);
            }

            resolve();
          }
        } catch (error) {
          console.error('❌ 解析响应失败:', error);
          resolve();
        }
      };

      this.ws.on('message', responseHandler);

      // 发送1102查询请求
      const requestData = {
        act_id: 1102,
        s: 1, // 1星副将
        t: 4, // 按攻击力排序
        pageNum: 0 // 第1页
      };

      console.log('📤 发送1102查询请求:');
      console.log(JSON.stringify(requestData, null, 2));

      this.ws.send(JSON.stringify(requestData));

      // 设置超时
      setTimeout(() => {
        if (!responseReceived) {
          this.ws.removeListener('message', responseHandler);
          console.log('⏰ 请求超时');
          resolve();
        }
      }, 5000);
    });
  }

  async run () {
    try {
      await this.connect();
      await this.test1102Response();
    } catch (error) {
      console.error('❌ 测试失败:', error.message);
    } finally {
      if (this.ws) {
        this.ws.close();
        console.log('🔌 WebSocket连接已关闭');
      }
    }
  }
}

// 运行测试
const tester = new Code1102ResponseTester();
tester.run();
